<template>
  <div class="app-container">

    <div class="button" style="padding-bottom:0.5rem">
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item title="借还情况查询" name="1">
          <el-row style="padding-bottom: 1rem">
<!--            <el-col :span="6">-->
<!--              <span>请选择用户:</span>-->
<!--              <el-cascader-->
<!--                style="width:60%"-->
<!--                placeholder="请选择用户"-->
<!--                v-model="userName"-->
<!--                :options="userList"-->
<!--                :props="{value:'depId',label:'depName',children:'userList'}"-->
<!--                collapse-tags-->
<!--                filterable-->
<!--                clearable-->
<!--              ></el-cascader>-->
<!--            </el-col>-->
            <el-col :span="6">
              <span>手机号:</span>
              <el-input v-model="telephone" type="text" style="width: 60%" placeholder="请输入手机号"></el-input>
            </el-col>
            <el-col :span="12">
              <span>请选择日期:</span>
              <el-date-picker
                v-model="beginTime"
                type="date"
                style="width:40%"
                placeholder="选择日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
              {{"-"}}
              <el-date-picker
                v-model="endTime"
                style="width:40%"
                type="date"
                placeholder="选择日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-row>
          <el-row>
<!--            <el-col :span="8">-->
<!--              <span>请选择设备类型:</span>-->
<!--              <el-select v-model="typeId" placeholder="请选择" style="width:60%">-->
<!--                <el-option-->
<!--                  v-for="item in typeList"-->
<!--                  :key="item.typeId"-->
<!--                  :label="item.typeName"-->
<!--                  :value="item.typeId"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-col>-->
            <el-col :span="8">
              <span>请选择设备:</span>
              <el-select v-model="deviceName" filterable clearable  placeholder="请选择" style="width:60%">
                <el-option
                  v-for="item in deviceList"
                  :key="item.deviceName"
                  :label="item.deviceName"
                  :value="item.deviceName"
                ></el-option>
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-button type="primary" @click="find(pageIndex=1)">查询</el-button>
              <el-button type="primary" @click="restart">重置</el-button>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>

    </div>

    <el-table
      element-loading-text="Loading"
      :data="broRetList"
      border
      fit
      style="width: 100%"
      highlight-current-row
    >
      <el-table-column label="出借设备名" align="center" width="150rem" fixed>
        <template slot-scope="scope">{{ scope.row.deviceName }}</template>
      </el-table-column>
      <el-table-column label="出借人" align="center" width="150rem">
        <template slot-scope="scope">{{ scope.row.userName}}</template>
      </el-table-column>
      <el-table-column label="手机号" align="center" width="150rem">
        <template slot-scope="scope">{{ scope.row.telephone }}</template>
      </el-table-column>
      <el-table-column label="借出时间" align="center" width="200rem">
        <template slot-scope="scope">{{ scope.row.borrowTime }}</template>
      </el-table-column>
      <el-table-column label="出借人照片" align="center" width="150rem">
<!--        <template slot-scope="scope">{{ scope.row.borrowImage }}</template>-->
        <template slot-scope="scope">
          <!--          {{ scope.row.borrowImage }}-->
          <el-image :src="byteToArray(scope.row.borrowImage)"/>
        </template>
      </el-table-column>
      <el-table-column label="归还时间" align="center" width="200rem">
        <template slot-scope="scope">{{ scope.row.retTime }}</template>
      </el-table-column>
      <el-table-column label="归还人照片" align="center" width="150rem">
        <template slot-scope="scope">
          <!--          {{ scope.row.retImage }}-->
          <el-image :src="byteToArray(scope.row.retImage)"/>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      align="center"
    ></el-pagination>

  </div>
</template>

<script>
  import $ from "@/api/broRetInquiry"

  export default {
    data() {
      return {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        broRetList: [],
        // 员工列表
        userList: [],
        // 设备列表
        deviceList: [],
        // 设备类型列表
        typeList: [],
        beginTime: "",
        endTime: "",
        telephone: "",
        userName: "",
        deviceName: "",
        typeId: "",
        activeName: "1",
      }
    },
    created() {
      this.fetchData()
      $.tbTypeList().then(res => {
        if (res.success) {
          this.typeList = res.data
        }
      })
      $.tbDeviceList().then(res => {
        if (res.success) {
          this.deviceList = res.data.list

          // console.log(this.deviceList)
        }
      })
      // $.dmUserDtoList().then(res => {
      //   if (res.success) {
      //     this.userList = res.data
      //
      //     // console.log(this.userList)
      //   }
      // })
    },
    methods: {
      byteToArray(path){
        let reg = new RegExp('\\\\',"g")//g,表示全部替换。
        path=path.replace(reg,"/");
        return "http://localhost:8080/tb-user/imgInfo?path="+path
      },
      fetchData() {
        this.currentPage = 1
        $.borrowDtoList({
          pageIndex: this.currentPage,
          pageSize: this.pageSize
        }).then(res => {
          this.broRetList = res.data.list
          this.total = res.data.total;
          console.log(this.broRetList)
        })
      },
      restart() {
        this.beginTime = ""
        this.endTime = ""
        this.userName = ""
        this.deviceName = ""
        this.typeId = ""
        this.fetchData()
      },

      // 翻页查看
      find(pageIndex) {
        if (!this.validate()) return;
        $.borrowDtoList({
          pageIndex: pageIndex,
          pageSize: this.pageSize,
          beginTime: this.beginTime,
          endTime: this.endTime,
          userName: this.userName,
          telephone: this.telephone,
          deviceName: this.deviceName,
          typeId: this.typeId
        }).then(res => {
          this.broRetList = res.data.list
          this.total = res.data.total;
          // for (var i = 0; i < this.administratorList.length; i++) {
          //   this.$set(this.administratorList[i], "a", i + 1);
          // }

        })
      },

      handleSizeChange(val) {
        this.pageSize = val;
        this.currentPage = 1;
        if (this.state == 1) {
          this.find(this.currentPage)
        } else this.find(this.currentPage)
      },

      handleCurrentChange(val) {
        this.currentPage = val;
        //console.log(val)
        if (this.state == 1) {
          this.find(this.currentPage)
        } else this.find(this.currentPage)
      },
      validate() {
        let error = "";
        if (!/^1[0-9]{10}$/.test(this.telephone)&&this.telephone!="") {
          error = "请输入正确的手机号\n";
          this.telephone = ""
        } else if (this.beginTime==""&&this.endTime!=""||this.beginTime!=""&&this.endTime==""||this.beginTime!=""&&this.endTime!=""&&this.beginTime >= this.endTime) {
          error = "请输入正确的时间范围\n";
          this.beginTime = ""
          this.endTime = ""
        }
        if (error) {
          this.$message({
            message: error,
            type: "error"
          });
          return false;
        }
        return true;
      },
    }
  }
</script>
